package com.cgsd.heyu.widgets.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.android.library.utils.BeanUtils;
import com.android.library.utils.DpPxUtil;
import com.cgsd.heyu.R;
import com.cgsd.heyu.ad.AdBanner;
import com.cgsd.heyu.entity.AdContentEntity;
import com.cgsd.heyu.http.ErrorMessage;
import com.cgsd.heyu.presenter.home.HomeContract;
import com.cgsd.heyu.presenter.home.HomePresenter;

/**
 * @author xiaoxing
 * Description 新用户奖励
 * CreateData: 2020/11/3
 */
public class ShareRewardDialog extends Dialog implements View.OnClickListener,HomeContract.RewordDialogView {
    private Context mContext;
    private TextView tvFlowerNum;
    private FrameLayout mFrameLayout;
    private HomeContract.Presenter mPresenter;
    private String type;
    private int width;
    private int height;
    private AdBanner adBanner;
    private LinearLayout mLLScore;
    private TextView mTvTitle;

    public ShareRewardDialog(@NonNull Context context,String type) {
        super(context, R.style.dialog_custom);
        this.mContext = context;
        this.type = type;
        setContentView(R.layout.dialog_share_reward);
        Window window = this.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
        }
        setCanceledOnTouchOutside(false);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        // 获取屏幕宽、高用
        DisplayMetrics d = mContext.getResources().getDisplayMetrics();
        // 宽度设置为屏幕的0.7
        lp.width = (int) (d.widthPixels * 0.8);
        width = (int) (d.widthPixels * 0.8);
        height = width * 150 / 600;
        dialogWindow.setAttributes(lp);
        initView();
    }

    private void initView() {
        new HomePresenter(this);
        mFrameLayout = findViewById(R.id.mFrameLayout);
        tvFlowerNum = findViewById(R.id.tv_flower_num);
        mLLScore = findViewById(R.id.ll_score);
        mTvTitle = findViewById(R.id.tv_title);
        findViewById(R.id.iv_delete).setOnClickListener(this);

        mPresenter.getAdContent(type);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_delete){
            dismiss();
        }
    }

    public void setFlowerNum(String num){
        tvFlowerNum.setText(num);
    }

    public void setCashTip(){
        mLLScore.setVisibility(View.INVISIBLE);
        mTvTitle.setText("提现申请成功，请耐心等待审核");
    }


    @Override
    public void setAdContent(AdContentEntity adContent, String type) {
        if (adContent == null){
            return;
        }
        adBanner = new AdBanner((Activity) mContext,adContent,mFrameLayout, DpPxUtil.px2dip(mContext,width),DpPxUtil.px2dip(mContext,height));
    }

    @Override
    public void setPresenter(HomeContract.Presenter presenter) {
        mPresenter = BeanUtils.checkNotNull(presenter);
    }

    @Override
    public void setErrorMessage(ErrorMessage error) {

    }

    @Override
    public void show() {
        super.show();
        if (adBanner != null){
            adBanner.clear();
        }
        if (mPresenter != null) {
            mPresenter.getAdContent(type);
        }
    }

}
